package cn.jobstart.web.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.Map;

@Component
public class HttpUtil {

    @Resource
    private SkRestTemplate restTemplate;

    private static int tryMaxCount=5;


    public Map<String, Object> httpGet(String url) {

        UriComponentsBuilder builder = UriComponentsBuilder
                .fromHttpUrl(url);

       /* restTemplate.getMessageConverters()
                .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));*/
        String result = null;

        try {
            result = restTemplate.getForObject(builder.build().encode(Charset.forName("UTF-8")).toUri(), String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        JSONObject json = JSONObject.parseObject(result);
        Map a = (Map) (json.get("data"));//.get("data");
        return a;
    }

    public JSONObject httpGetJsonObject(String url) {

        return httpGetJsonObject(url, null);
    }

    public JSONObject httpGetJsonObject(String url, HttpHeaders headers) {

        int i=0;
        while (i++<tryMaxCount) {

            String result = httpGetString(url, headers);
            if (result != null) {
                return JSONObject.parseObject(result);
            }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
        }
        return null;


    }

    public JSONArray httpGetJsonArray(String url) {

        return JSONArray.parseArray(httpGetString(url));
    }

    public String httpGetString(String url) {
        return httpGetString(url, null);
    }

    public String httpGetString(String url, HttpHeaders headers) {
        UriComponentsBuilder builder = UriComponentsBuilder
                .fromHttpUrl(url);

        // restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));


        String result = null;
        try {
            result = restTemplate.getForObject(builder.build().encode(Charset.forName("UTF-8")).toUri(), String.class,headers);
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Map httpPutReturnMap(String url, Object paras) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(paras.toString(), headers);
        ResponseEntity<Map> resp = restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class);
        Map result = resp.getBody();
        return result;
    }

    public SkMap httpPut(String url, Object paras) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(paras.toString(), headers);
        return restTemplate.exchange(url, HttpMethod.PUT, entity, SkMap.class).getBody();
    }

    public SkMap httpPost(String url, Object paras) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(paras.toString(), headers);
        SkMap result = restTemplate.postForEntity(url, entity, SkMap.class).getBody();
        return result;
    }


    public SkMap httpDelete(String url) {
        UriComponentsBuilder builder = UriComponentsBuilder
                .fromHttpUrl(url);
       /* restTemplate.getMessageConverters()
                .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));*/
        try {
            return restTemplate.exchange(builder.build().encode(Charset.forName("UTF-8")).toUri(), HttpMethod.DELETE, null, SkMap.class).getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    public JSONObject httpDeleteJson(String url) {
        UriComponentsBuilder builder = UriComponentsBuilder
                .fromHttpUrl(url);
      /*  restTemplate.getMessageConverters()
                .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));*/
        try {
            return restTemplate.exchange(builder.build().encode(Charset.forName("UTF-8")).toUri(), HttpMethod.DELETE, null, JSONObject.class).getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
            return null;
        }
    }

    public JSONObject httpPutJson(String url, Object paras) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(paras.toString(), headers);
        return restTemplate.exchange(url, HttpMethod.PUT, entity, JSONObject.class).getBody();
    }



    public JSONObject tryHttpPostJson(String url, Object paras, HttpHeaders headers) {

        int i=0;
        while (i++<tryMaxCount){

            JSONObject jsonObject=httpPostJson(url,paras,headers);
            if(jsonObject!=null) {
                return jsonObject;
            }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }


        }
        return null;

    }

    public JSONObject httpPostJson(String url, Object paras, HttpHeaders headers) {
        try {
            HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
            httpRequestFactory.setConnectionRequestTimeout(30000);
            httpRequestFactory.setConnectTimeout(30000);
            httpRequestFactory.setReadTimeout(30000);


            restTemplate.setRequestFactory(httpRequestFactory);

            if (headers == null) {
                headers = new HttpHeaders();
            }
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

            HttpEntity<String> entity = null;
            if (paras != null) {
                entity = new HttpEntity<String>(paras.toString(), headers);
            } else {
                entity = new HttpEntity<String>("", headers);
            }


            JSONObject result = restTemplate.postForEntity(url, entity, JSONObject.class).getBody();


            return result;
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject httpPostJson(String url, Object paras) {
        return httpPostJson(url, paras, null);
    }

}
